package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPersonalAnalysis

import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPersonalAnalysis.AibkAnalysisAnswerSheetChildAdapter.OnClickChildListener
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import android.text.Html
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.PaperPerAnalysisInfor
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import com.lancoo.znbkxx.teacher.utils.SpacesItemDecorationX
import kotlinx.android.synthetic.main.aibk_adapter_answersheet_parent.view.*

class AibkAnalysisAnswerSheetParentAdapter(
        val activity: AppCompatActivity,
        val paperPerAnalysisInfor: PaperPerAnalysisInfor
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_answersheet_parent, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {

        val typeIndex = paperPerAnalysisInfor.TypeList[position].TypeIndex
        val strTypeIndex = LancooMethod.numberToChinese(typeIndex)
        var genreName = paperPerAnalysisInfor.TypeList[position].GenreName
        val typeName = paperPerAnalysisInfor.TypeList[position].TypeName
        if (genreName.isEmpty()) {
            genreName = typeName
        }
        var evalScore = paperPerAnalysisInfor.TypeList[position].EvalScore
        var totalScore = paperPerAnalysisInfor.TypeList[position].TotalScore
        //显示分数及准确率颜色
        if (evalScore == "--") {
            holder.convertView.TvTypeInfor.text =
                    Html.fromHtml(activity.resources.getString(R.string.aibk_answersheet_ques_infor_blue,
                            strTypeIndex, genreName, evalScore, totalScore))
        } else {
            val correctRate = LancooMethod.getCorrectRate(evalScore.toDouble(), totalScore.toDouble())
            if (correctRate >= 0.6) {
                holder.convertView.TvTypeInfor.text =
                        Html.fromHtml(activity.resources.getString(R.string.aibk_answersheet_ques_infor_green,
                                strTypeIndex, genreName, evalScore, totalScore))
            } else {
                holder.convertView.TvTypeInfor.text =
                        Html.fromHtml(activity.resources.getString(R.string.aibk_answersheet_ques_infor_red,
                                strTypeIndex, genreName,evalScore, totalScore))
            }
        }

        /*******************************************************************************************/
        val sortIndex = paperPerAnalysisInfor.TypeList[position].QuesList[0].SortIndexList[0].SortIndex
        if(sortIndex.contains("-")){
            val space = DensityUtils.dp2px(8f)
            holder.convertView.RvAnswerSheetChild.addItemDecoration(SpacesItemDecorationX(space))
            holder.convertView.RvAnswerSheetChild.layoutManager = GridLayoutManager(activity, 4)
        }else{
            val space = DensityUtils.dp2px(0f)
            holder.convertView.RvAnswerSheetChild.addItemDecoration(SpacesItemDecorationX(space))
            holder.convertView.RvAnswerSheetChild.layoutManager = GridLayoutManager(activity, 6)
        }
        /*******************************************************************************************/

        val adapterChild = AibkAnalysisAnswerSheetChildAdapter(
                activity, paperPerAnalysisInfor, position
        )
        holder.convertView.RvAnswerSheetChild.adapter = adapterChild
        adapterChild.setClickChildListener(object : OnClickChildListener {
            override fun onClick(
                    index_A: Int,
                    index_B: Int,
                    index_C: Int
            ) {
                clickParentListener!!.onClick(index_A, index_B,index_C)
            }
        })


    }

    override fun getItemCount(): Int {
        return paperPerAnalysisInfor.TypeList.size
    }

    interface OnClickParentListener {
        fun onClick(index_A: Int, index_B: Int, index_C: Int)
    }

    private var clickParentListener: OnClickParentListener? = null

    fun setClickParentListener(clickParentListener: OnClickParentListener?) {
        this.clickParentListener = clickParentListener
    }

}